package com.fxu.jushuitan.dto;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fxu.framework.core.exception.MsgException;
import com.fxu.framework.core.util.BeanUtil;
import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 所有通用返回
 * @author fangxu
 * @since 2023-08-18
 * @version 1.0.0
 **/
@Data
public class RootRes {
    private static Integer SUCCESS = 0;
    /**
     * 编号
     */
    private Integer code;

    /**
     * 返回结果描述
     */
    private String msg;

    /**
     * 返回结果
     */
    private Object data;

    /**
     * 是否成功
     */
    private boolean isSuccess() {
        return null != this.code && Objects.equals(SUCCESS, this.code);
    }

    public static <E> E toBean(String body, Class<E> clazz) {
        if (StrUtil.isEmpty(body) || clazz == null) return null;
        RootRes res = JSONUtil.toBean(body, RootRes.class);
        if (!res.isSuccess()) {
            throw new MsgException("解析错误!body=" + body);
        }
        return res.data == null ? null : BeanUtil.toBean(res.data, clazz);
    }

    @SuppressWarnings("unchecked")
    public static <E> List<E> toList(String body, Class<E> clazz) {
        if (StrUtil.isEmpty(body) || clazz == null) return null;
        RootRes res = JSONUtil.toBean(body, RootRes.class);
        if (!res.isSuccess()) {
            throw new MsgException("解析错误!body=" + body);
        }
        if (res.data == null) return null;
        if (!(res.data instanceof Collection)) {
            JSONObject jsonObject = JSONUtil.parseObj(res.data.toString());
            JSONArray dataList = jsonObject.getJSONArray("datas");
            if (CollUtil.isNotEmpty(dataList)) {
                return dataList.toList(clazz);
            } else {
                return CollUtil.toList(jsonObject.toBean(clazz));
            }
        }
        Collection<Object> list = (Collection<Object>) res.data;
        return list.stream().filter(Objects::nonNull).map(d -> BeanUtil.toBean(d, clazz)).collect(Collectors.toList());
    }

    @SuppressWarnings("unchecked")
    public static <E> PageRes<E> toPage(String body, Class<E> clazz) {
        return toPage(body, clazz, "datas");
    }

    @SuppressWarnings("unchecked")
    public static <E> PageRes<E> toPage(String body, Class<E> clazz, String prop) {
        if (StrUtil.isEmpty(body) || clazz == null) return null;
        RootRes res = JSONUtil.toBean(body, RootRes.class);
        if (!res.isSuccess()) {
            throw new MsgException("解析错误!body=" + body);
        }
        if (res.data == null) return null;
        PageRes<E> pageRes = BeanUtil.toBean(res.data, PageRes.class);
        JSONObject jsonObject = JSONUtil.parseObj(res.data.toString());
        JSONArray orders = jsonObject.getJSONArray(prop);
        if (orders != null) {
            pageRes.setDataList(orders.toList(clazz));
        }
        return pageRes;
    }

    public static RootRes to(String body) {
        if (StrUtil.isEmpty(body)) return null;
        return JSONUtil.toBean(body, RootRes.class);
    }
}